using System.Collections.Generic;
using Game.Const;
using Game.Core;
using Game.Main.Model;
using Game.Main.View;
using Game.Manager;
using Game.XH;
using SimpleJSON;
using UnityEngine;

namespace Game.Component
{
	public class ElementCreatorItem : BaseItem
	{
		private enum Anim
		{
			Static = 0,
			Clamp = 1
		}

		private enum BtnAnim
		{
			Staic = 0,
			Spring = 1
		}

		private enum TouchType
		{
			None = 0,
			Electric = 1
		}

		private float MIN_ROCKER_ANGLE = -30f;

		private float MAX_ROCKER_ANGLE = 30f;

		private float SPRING_START_VAL = 0.7f;

		private float SPRING_MINUS_VAL = 0.03f;

		private float MIN_CLAMP_X = -1.5f;

		private float NORMAL_CLAMP_X = -1.16f;

		private float MAX_CLAMP_X = 0.36f;

		private float MIN_CLAMP_Y = 2.8f;

		private float MAX_CLAMP_Y = 3.8f;

		private float CLAMP_NORMAL_ANGLE;

		private float CLAMP_TAKE_ANGLE = 20f;

		private BtnAnim btnAnim;

		private bool springUp;

		private float springVal;

		private float springCurVal;

		private float springEndVal;

		private float springSpeed;

		private Anim anim;

		private float animTimer;

		private int animStage;

		private Transform btnElectric;

		private Transform btnElectricSpring;

		private Transform btnElectricSprite;

		private Transform clamp;

		private Transform clampPoint;

		private Transform leftClamp;

		private Transform rightClamp;

		private Transform elements;

		private Transform clampElement;

		private Transform checkPoint;

		private bool isTouchRocker;

		private float touchTimer;

		private int selectElementIndex;

		private bool isPlayElectricEfc;

		private float electricTimer;

		private List<int> useIndexs = new List<int>();

		private Transform touchElement;

		private bool isTouchBtnElectric;

		public override void Init(ItemArchiveData data)
		{
			base.Init(data);
			btnElectric = frontMask.Find("BtnElectric");
			btnElectric.gameObject.layer = 15;
			btnElectricSpring = btnElectric.Find("Spring");
			btnElectricSprite = btnElectric.Find("Sprite");
			clamp = model.Find("Clamp");
			leftClamp = clamp.Find("Left");
			rightClamp = clamp.Find("Right");
			clampPoint = clamp.Find("Point");
			elements = model.Find("Elements");
			int childCount = elements.childCount;
			for (int i = 0; i < childCount; i++)
			{
				Transform child = elements.GetChild(i);
				child.gameObject.layer = 15;
			}
			checkPoint = model.Find("CheckPoint");
			if (data.intList != null)
			{
				useIndexs = data.intList;
			}
			selectElementIndex = data.state;
			CreateSelectElement(true);
			InitElements();
		}

		private void InitElements()
		{
			int childCount = elements.childCount;
			for (int i = 0; i < childCount; i++)
			{
				if (useIndexs.Contains(i))
				{
					elements.GetChild(i).gameObject.SetActive(false);
				}
			}
		}

		public bool ChangeElectricMachine(BaseItem item)
		{
			return false;
		}

		private void InitComponentTrigger(GameObject obj, ItemComponentTrigger.Type type)
		{
			obj.layer = 12;
			ItemComponentTrigger itemComponentTrigger = obj.AddComponent<ItemComponentTrigger>();
			itemComponentTrigger.Init(this, type, null, null, null);
		}

		protected override void LateUpdate()
		{
			if (btnAnim == BtnAnim.Spring)
			{
				springCurVal = Mathf.MoveTowards(springCurVal, springEndVal, springSpeed * Time.deltaTime * 4f);
				SetBtnScale(springCurVal);
				if (springCurVal == springEndVal)
				{
					springVal -= SPRING_MINUS_VAL;
					if (springUp)
					{
						springEndVal = 1f - springVal;
						springSpeed = springCurVal - springEndVal;
					}
					else
					{
						springEndVal = 1f + springVal;
						springSpeed = springEndVal - springCurVal;
					}
					springUp = !springUp;
					if (springVal < 0f)
					{
						btnAnim = BtnAnim.Staic;
					}
				}
			}
			if (isPlayElectricEfc)
			{
				electricTimer += Time.deltaTime;
				if (electricTimer >= 2f)
				{
					isPlayElectricEfc = false;
					if (clampElement != null && anim == Anim.Static)
					{
						string id = clampElement.name.Substring(0, clampElement.name.Length - 1) + Random.Range(2, 4);
						BaseItem baseItem = Singleton<Scene>.Instance.CreateItem(id, clampElement.position, 0);
						if (baseItem != null)
						{
							baseItem.DropDown(checkPoint.position, true);
							Singleton<AudioManager>.Instance.PlaySound("sound_explode");
							Singleton<EffectManager>.Instance.ShowEffect(PathConst.EFFECT + "efc_explode_1", baseItem.transform.position + new Vector3(0f, 0.1f, -0.0004f), 1f);
						}
						Object.Destroy(clampElement.gameObject);
						clampElement = null;
						selectElementIndex = 0;
						leftClamp.SetLocalRotationZ(CLAMP_NORMAL_ANGLE);
						rightClamp.SetLocalRotationZ(CLAMP_NORMAL_ANGLE);
					}
				}
			}
			if (anim == Anim.Static || !(touchElement != null))
			{
				return;
			}
			if (animStage == 0)
			{
				clamp.SetLocalX(Mathf.MoveTowards(clamp.localPosition.x, touchElement.localPosition.x, Time.deltaTime * 0.5f));
				if (clamp.localPosition.x == touchElement.localPosition.x)
				{
					animStage++;
					animTimer = 0f;
				}
			}
			else if (animStage == 1)
			{
				animTimer += Time.deltaTime;
				if (animTimer >= 0.5f)
				{
					animStage++;
					animTimer = 0f;
					Singleton<AudioManager>.Instance.PlaySound("sound_element_creator_2");
				}
			}
			else if (animStage == 2)
			{
				animTimer += Time.deltaTime;
				clamp.SetLocalY(Mathf.Lerp(MAX_CLAMP_Y, MIN_CLAMP_Y, animTimer));
				if (animTimer >= 1f)
				{
					animStage++;
					animTimer = 0f;
				}
			}
			else if (animStage == 3)
			{
				animTimer += Time.deltaTime;
				if (animTimer >= 0.5f)
				{
					animStage++;
					animTimer = 0f;
					Singleton<AudioManager>.Instance.PlaySound("sound_element_creator_1");
				}
			}
			else if (animStage == 4)
			{
				animTimer += Time.deltaTime * 2f;
				leftClamp.SetLocalRotationZ(Mathf.Lerp(0f, CLAMP_TAKE_ANGLE, animTimer));
				rightClamp.SetLocalRotationZ(Mathf.Lerp(0f, 0f - CLAMP_TAKE_ANGLE, animTimer));
				if (animTimer >= 1f)
				{
					animTimer = 0f;
					animStage++;
					SelectElement();
					CreateSelectElement(false);
				}
			}
			else if (animStage == 5)
			{
				animTimer += Time.deltaTime * 2f;
				if (clampElement == null)
				{
					leftClamp.SetLocalRotationZ(Mathf.Lerp(CLAMP_TAKE_ANGLE, 0f, animTimer));
					rightClamp.SetLocalRotationZ(Mathf.Lerp(0f - CLAMP_TAKE_ANGLE, 0f, animTimer));
				}
				else
				{
					clampElement.localPosition = Vector2.MoveTowards(clampElement.localPosition, Vector2.zero, Time.deltaTime);
				}
				if (animTimer >= 1f)
				{
					animTimer = 0f;
					animStage++;
					Singleton<AudioManager>.Instance.PlaySound("sound_element_creator_2");
				}
			}
			else if (animStage == 6)
			{
				animTimer += Time.deltaTime;
				clamp.SetLocalY(Mathf.Lerp(MIN_CLAMP_Y, MAX_CLAMP_Y, animTimer));
				if (animTimer >= 1f)
				{
					animStage++;
					animTimer = 0f;
					Singleton<AudioManager>.Instance.PlaySound("sound_element_creator_1");
				}
			}
			else if (animStage == 7)
			{
				animTimer += Time.deltaTime;
				clamp.SetLocalX(Mathf.MoveTowards(clamp.localPosition.x, NORMAL_CLAMP_X, Time.deltaTime * 0.5f));
				if (clamp.localPosition.x == NORMAL_CLAMP_X)
				{
					animStage = 0;
					animTimer = 0f;
					anim = Anim.Static;
					touchElement = null;
				}
			}
		}

		private void CreateSelectElement(bool isPlaceZero)
		{
			if (selectElementIndex <= 0)
			{
				return;
			}
			Transform child = elements.GetChild(selectElementIndex - 1);
			if (child != null)
			{
				clampElement = Object.Instantiate(child.gameObject).transform;
				clampElement.SetParent(clampPoint);
				clampElement.localScale = Vector3.one;
				if (isPlaceZero)
				{
					leftClamp.SetLocalRotationZ(CLAMP_TAKE_ANGLE);
					rightClamp.SetLocalRotationZ(0f - CLAMP_TAKE_ANGLE);
					clampElement.localPosition = Vector3.zero;
				}
				else
				{
					clampElement.position = child.position;
					clampElement.SetLocalZ(0f);
				}
				clampElement.name = child.name;
				clampElement.gameObject.SetActive(true);
				child.gameObject.SetActive(false);
			}
		}

		public override bool AttachItem(BaseItem item, DepthType depthType, AttachType attachType, bool updatePosition)
		{
			bool flag = base.AttachItem(item, depthType, attachType, updatePosition);
			if (flag && item.itemData.scriptType == 133)
			{
				item.SetAllCollidersEnabled(false);
			}
			return flag;
		}

		public override void ReleaseAttachItem(BaseItem item)
		{
			base.ReleaseAttachItem(item);
			if (item.itemData.scriptType == 133)
			{
				item.SetAllCollidersEnabled(true);
			}
		}

		private void SelectElement()
		{
			int num = -1;
			int childCount = elements.childCount;
			for (int i = 0; i < childCount; i++)
			{
				Transform child = elements.GetChild(i);
				if (child.gameObject.activeSelf && touchElement == child)
				{
					num = i;
				}
			}
			if (num != -1)
			{
				selectElementIndex = num + 1;
				useIndexs.Add(num);
			}
		}

		public override void QuicklyTouch(TouchEventData touchEventData)
		{
			base.QuicklyTouch(touchEventData);
			if (anim == Anim.Static && !(clampElement != null))
			{
				RaycastHit2D raycastHit2D = Physics2D.Raycast(touchEventData.curPos, Vector2.zero, 15f, 32768);
				if (raycastHit2D.collider != null && raycastHit2D.collider.transform.parent == elements)
				{
					touchElement = raycastHit2D.collider.transform;
					anim = Anim.Clamp;
					animStage = 0;
					animTimer = 0f;
					Singleton<AudioManager>.Instance.PlaySound("sound_element_creator_1");
				}
			}
		}

		public override void TouchDown(TouchEventData touchEventData)
		{
			base.TouchDown(touchEventData);
			isTouchBtnElectric = false;
			RaycastHit2D raycastHit2D = Physics2D.Raycast(touchEventData.curPos, Vector2.zero, 15f, 32768);
			if (raycastHit2D.collider != null && raycastHit2D.collider.transform == btnElectric && !isPlayElectricEfc)
			{
				isTouchBtnElectric = true;
				btnAnim = BtnAnim.Staic;
				SetBtnScale(SPRING_START_VAL);
				isPlayElectricEfc = true;
				electricTimer = 0f;
				Transform transform = model.Find("ElectriPoint");
				if (transform != null)
				{
					Singleton<EffectManager>.Instance.ShowEffect(PathConst.EFFECT + base.itemData.effect, new Vector3(transform.transform.position.x, transform.transform.position.y, clampPoint.position.z - 5E-05f), 2f);
				}
				PlayElectricSound();
				Singleton<AudioManager>.Instance.PlaySound("sound_btn_1");
			}
		}

		private void PlayElectricSound()
		{
			Singleton<AudioManager>.Instance.PlaySound("sound_electric_1");
		}

		public override void PlayQuicklyClickSound()
		{
		}

		private void SetBtnScale(float val)
		{
			btnElectricSpring.SetLocalScaleY(val);
			btnElectricSprite.SetLocalY(0.455f * val);
		}

		public override void TouchUp(TouchEventData touchEventData)
		{
			base.TouchUp(touchEventData);
			if (isTouchBtnElectric)
			{
				isTouchBtnElectric = false;
				btnAnim = BtnAnim.Spring;
				springUp = true;
				springVal = 1f - SPRING_START_VAL - SPRING_MINUS_VAL;
				springCurVal = SPRING_START_VAL;
				springEndVal = 1f + springVal;
				springSpeed = springEndVal - springCurVal;
			}
		}

		public override void PackData(JSONObject obj)
		{
			base.PackData(obj);
			obj["33"] = selectElementIndex;
			obj["48"] = PackIntList(useIndexs);
		}
	}
}
